function httpAction(){
    let p =new Promise(function(resolve,reject){
        setTimeout(() => {
            resolve('request completed')
        }, 3000);
    })
    return p;
}
function httpErrAction(){
    let p =new Promise(function(resolve,reject){
        setTimeout(() => {
            reject('request failed')
        }, 4000);
    })
    return p;
}

function testApi(){
    let p = new Promise(function(resolve,reject){
        setTimeout(()=>{
            resolve('接口请求超过2秒')
        }, 2000)
    })
    return p
}

// Promise.race只返回第一个执行完毕的promise的结果，无论结果是fullfilled还是rejected
Promise.race([httpAction(),httpErrAction()]).then(res => {
    console.log(res);   
  }).catch(err=>{
      console.log(err);    
  });

//   Promise.race可以用于测试接口返回速度
  Promise.race([httpAction(),testApi()]).then(res => {
    console.log(res);   
  }).catch(err=>{
      console.log(err);    
  });
